<?php
// +----------------------------------------------------------------------
// | INPHP
// | Copyright (c) 2023 https://inphp.cc All rights reserved.
// | Licensed ( https://opensource.org/licenses/MIT )
// | Author: 幺月儿(https://gitee.com/lulanyin) Email: inphp@qq.com
// +----------------------------------------------------------------------
// | 配置获取、修改保存
// +----------------------------------------------------------------------
namespace app\admin\http\api;

use Inphp\Core\Object\Message;
use Inphp\Core\Util\File;
use Inphp\Core\Util\Str;

#[\app\admin\attributes\auth(null, true)] class config
{
    /**
     * 获取配置原文，自动转化为JSON了，客户端请注意判断使用
     * @return Message
     */
    public function index(): Message
    {
        $name = GET("name", "admin");
        $config = [];
        if ($name === "admin") {
            $config = \Inphp\Core\Modules::getModule("admin")->getConfig();
            $config = is_array($config) ? $config : [];
            unset($config["auth"]);
        } else {
            $dir = \Inphp\Core\Config::get("define.configs");
            $dir .= "/public";
            $files = File::getFiles($dir, "php");
            foreach ($files as $file) {
                $filename = substr($file["filename"], 0, stripos($file["filename"], "."));
                if ($name === $filename) {
                    $config = include $file["path"];
                }
            }
        }
        return httpMessage(0, "success", $config);
    }

    /**
     * 保存配置
     * @return Message
     */
    public function save(): Message
    {
        $config = POST("config");
        $config = is_array($config) ? $config : (@json_decode($config) ?? []);
        if (empty($config)) {
            return httpMessage("缺少配置参数");
        }
        $adminHome = $config["adminHome"] ?? null;
        $store = $config["store"] ?? [];
        $disableIP = $config["disableIP"] ?? "";
        $sc = [];
        $sc["enable"] = isset($store["enable"]) && ($store["enable"] || $store["enable"] == 1);
        $sc["url"] = $store["url"] ?? "https://inphp.cc/store";
        $sc["key"] = $store["key"] ?? "";
        //读取原配置
        $mc = \Inphp\Core\Modules::getModule("admin")->getConfig();
        $mc["adminHome"] = $adminHome;
        $mc["store"] = $sc;
        $mc["disableIP"] = $disableIP;
        $mc["php"] = $config["php"] ?? "php";
        if (!empty($mc["php"]) && stripos($mc["php"], "php") < 0) {
            return httpMessage("PHP脚本控制台程序路径必须含有php");
        }
        $mc["php"] = Str::trim($mc["php"]);
        $mc["composer"] = $config["composer"] ?? "composer";
        if (!empty($mc["composer"]) && stripos($mc["composer"], "composer") > 0) {
            return httpMessage("composer脚本控制台程序路径必须含有composer");
        }
        $mc["composer"] = Str::trim($mc["composer"]);
        if(!\Inphp\Core\Modules::getModule("admin")->saveConfig("config", $mc)) {
            return httpMessage("配置文件保存失败，请检测是否有写入权限！");
        }
        return httpMessage(0, "配置保存成功");
    }
}